package com.haynes.meetxiaohai.utils;

import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;

/**
 * 阿里云短信发送工具类
 */
public class SMSUtils {
	public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
		Config config = new Config()
				// 您的AccessKey ID
				.setAccessKeyId(accessKeyId)
				// 您的AccessKey Secret
				.setAccessKeySecret(accessKeySecret);
		// 访问的域名
		config.endpoint = "dysmsapi.aliyuncs.com";
		return new com.aliyun.dysmsapi20170525.Client(config);
	}
	/**
	 * 发送短信
	 *
	 * @param signName     签名
	 * @param templateCode 模板
	 * @param phoneNumbers 手机号
	 * @param param        参数
	 */
	public static void sendMessage(String signName, String templateCode, String phoneNumbers, String param) throws Exception {
		com.aliyun.dysmsapi20170525.Client client = SMSUtils.createClient("", "");
		com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
				.setPhoneNumbers(phoneNumbers)
				.setSignName(signName)
				.setTemplateCode(templateCode)
				.setTemplateParam("{\"code\":\""+param+"\"}");
		com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
		try {
			// 复制代码运行请自行打印 API 的返回值
			client.sendSmsWithOptions(sendSmsRequest, runtime);
		} catch (TeaException error) {
			// 如有需要，请打印 error
			com.aliyun.teautil.Common.assertAsString(error.message);
		}
	}
}
